<template>
  <div class="home-box">
    <!-- 顶部导航 -->
    <van-nav-bar fixed class="page-nav-bar">
      <van-button to="/search" class="search-btn" slot="title" icon="search" type="primary">搜索</van-button>
    </van-nav-bar>

    <!-- 标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in userChannels" :key="item.id" :title="item.name">
        <!-- 把对应标签的id传过去 -->
        <ArticalList :channelId="item.id" />
      </van-tab>
      <div @click="showChannelPopup=true" slot="nav-right" class="nav-right">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
      <div slot="nav-right" class="placeholder"></div>
    </van-tabs>
    <!-- 频道列表弹出层 -->
    <van-popup v-model="showChannelPopup" closeable position="bottom" close-icon-position="top-left"
      :style="{ height: '100%' }">
      <editChannel :channels="userChannels" :active="active" @closePopup="closePopup" />
    </van-popup>
  </div>
</template>

<script>
// 引入子组件
import ArticalList from './components/artical-list.vue'
import editChannel from './components/editChannel.vue'
import { getUseChannels } from '@/api/user.js'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'home',
  data () {
    return {
      // 频道列表弹出层
      showChannelPopup: false,
      active: 0,
      // 用户频道数据
      userChannels: []
    }
  },
  methods: {
    // 获取用户频道数据
    async loadUsersChannels () {
      // 用户未登录，本地没有数据的时候,进行判断
      if (!this.$store.state.user && getItem('hmttChannels')) {
        this.userChannels = getItem('hmttChannels')
      } else {
        try {
          const { data: res } = await getUseChannels()
          // console.log(res)
          this.userChannels = res.data.channels
        } catch (err) {
          // console.log(err)
          this.$toast('获取失败')
        }
      }
    },
    // 关闭弹层
    closePopup (e) {
      this.showChannelPopup = e.close
      this.active = e.index
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    ArticalList,
    editChannel
  },
  created () {
    this.loadUsersChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-box {
  padding-bottom: 100px;
  padding-top: 176px;
  /deep/.van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 2;
    width: 100%;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 32px;
    border: none;
  }
  /deep/ .van-icon-search {
    color: #fff;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }

  /deep/.channel-tabs {
    // 未选中的样式
    .van-tab {
      height: 82px;
      min-width: 200px;
      border-right: 1px solid #edeff3;
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }

    // 选中之后的样式
    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }

    // 条
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3296fa;
      border-radius: 3px;
      bottom: 8px;
    }

    // 把条的父盒子的padding去掉
    .van-tabs__nav {
      padding: 0;
    }

    // 右侧汉堡导航
    .nav-right {
      width: 66px;
      height: 82px;
      position: fixed;
      right: 0;
      background: rgba(255, 255, 255, 0.9);
      display: flex;
      align-items: center;
      justify-content: center;
      .toutiao {
        font-size: 33px;
      }
      &::before {
        content: '';
        width: 2px;
        height: 82px;
        background-image: url(~@/assets/gradient-gray-line.png);
        position: absolute;
        left: 0;
        background-size: 100% 100%;
      }
    }

    // 汉堡菜单背后的站位
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
